<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../../config.inc.php");
session_start();
$_LangPath = "searchwagerlist";
include_once(__Admin_Path."/logincheck_op.php");
include_once(__Language_Path."/selectlanguage_admin.php");
include_once(__Modules_Path."/BetType.lib.php");
include_once(__Modules_Path."/Lotto.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Modules_Path."/ChangeTime.lib.php");
include_once(__Modules_Path."/Gamble.lib.php");
include_once(__Common_Path."/Lotto.inc.php");

$arrPower = CheckUsePower("searchwagerlist");
if($arrPower === false)ErrMsgOutputHTML("Error", "", 1);
eregi("<!--searchwagerlist start-->(.*)<!--searchwagerlist end-->",strtolower($_SESSION["Admin_Info"] -> ProgramPower), $arrPower);


$intLID = (int)$_data["LottoID"];
if(empty($intLID))$intLID = (empty($_MemInitLottoID))?1:$_MemInitLottoID;

$isOpenGame = false;
$arrGD = getNewGambleData($intLID, 0);
if(is_array($arrGD)){
	if($arrGD["GameDate"] == date("Y-m-d") && $arrGD["EndGameTimeSN"] > date("H:i:s")){
		$isOpenGame = true;
		$_data["GambleID"] = $arrGD["ID"];
	}
}

if(is_array($_data)){
	foreach($_data as $key => $val){
		$_data[$key] = trim($val);
	}
}
if(!empty($_data["BetTime1"]))$_data["BetTime1"] = TimeForm(date("Y-m-d"), $_data["BetTime1"], "H:i:s");
if(!empty($_data["BetTime2"]))$_data["BetTime2"] = TimeForm(date("Y-m-d"), $_data["BetTime2"], "H:i:s");

if(!empty($_data["Mem"]) || !empty($_data["Sub"]) || !empty($_data["Main"]) || !empty($_data["Stock"])|| !empty($_data["Director"])){
	$arrUser = array();
	$strSQL = "select ID, Account, MemberGroup from `bet_Member` where ID = 0";
	if(!empty($_data["Mem"])){
		$strSQL .= " or (Account like '%".$_data["Mem"]."%' and MemberGroup = 1)";
		$arrUser[1][0] = 0;
	}
	if(!empty($_data["Sub"])){
		$strSQL .= " or (Account like '%".$_data["Sub"]."%' and MemberGroup = 2)";
		$arrUser[2][0] = 0;
	}
	if(!empty($_data["Main"])){
		$strSQL .= " or (Account like '%".$_data["Main"]."%' and MemberGroup = 3)";
		$arrUser[3][0] = 0;
	}
	if(!empty($_data["Stock"])){
		$strSQL .= " or (Account like '%".$_data["Stock"]."%' and MemberGroup = 4)";
		$arrUser[4][0] = 0;
	}
	if(!empty($_data["Director"])){
		$strSQL .= " or (Account like '%".$_data["Director"]."%' and MemberGroup = 5)";
		$arrUser[4][0] = 0;
	}
	//print "<!--".$strSQL."//-->\n";
	if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errorhidden"] = "Find User Data Error!";
		$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrUser[$Rows -> MemberGroup][$Rows -> ID] = $Rows -> ID;
	}
	$strSQL = "";
	if(is_array($arrUser[1]))$strSQL .= " and a.MemID in (".join(",", $arrUser[1]).")";
	if(is_array($arrUser[2]))$strSQL .= " and a.SubID in (".join(",", $arrUser[2]).")";
	if(is_array($arrUser[3]))$strSQL .= " and a.MainID in (".join(",", $arrUser[3]).")";
	if(is_array($arrUser[4]))$strSQL .= " and a.StockID in (".join(",", $arrUser[4]).")";
	if(is_array($arrUser[5]))$strSQL .= " and a.DirectorID in (".join(",", $arrUser[5]).")";
}

$arrBID = array();
$arrBet = array();
$arrParlay = array();
if($_data["BID"] != "")$arrBID = split(",", $_data["BID"]);

if(!empty($_data["Stake1"]) && !empty($_data["Stake2"]))$strSQL .= " and (a.MemStake between '".$_data["Stake1"]."' and '".$_data["Stake2"]."')";
if(!empty($_data["Result1"]) && !empty($_data["Result2"]))$strSQL .= " and (a.MemResult between '".$_data["Result1"]."' and '".$_data["Result2"]."')";
if(!empty($_data["BetTime1"]) && !empty($_data["BetTime2"]))$strSQL .= " and (b.BetTime between '".$_data["BetTime1"]."' and '".$_data["BetTime2"]."')";
if(!empty($_data["Bet_Type"]))$strSQL .= " and a.Bet_Type = '".$_data["Bet_Type"]."'";
if(!empty($_data["GambleID"]))$strSQL .= " and a.GambleID = '".$_data["GambleID"]."'";
if(!empty($_data["Number"]))$strSQL .= " and a.Number = '".$_data["Number"]."'";
if(!empty($_data["WagerType"]))$strSQL .= " and a.WagerType = '".$_data["WagerType"]."'";
if(!empty($_data["WagerStatus"]))$strSQL .= " and a.isPayOff = '".$_data["WagerStatus"]."'";
//if(!empty($intLID))$strSQL .= " and a.LottoID = '".$intLID."'";

if(sizeof($arrBID) > 0)$strSQL .= " and a.ID in (".join(",", $arrBID).")";
if($strSQL != ""){
	if($arrPower[1] != "all"){
		$strSQL .= " and a.WagerType = '1'";
	}
	$strSQLT = "select COUNT(*) as TotalItems from `bet_BetList` a, `bet_BetListContent` b where a.ID > 0 and a.ID = b.ID ".$strSQL;
	$strSQL = "select a.ID, a.LottoID, b.BetDate, b.BetTime, a.Bet_Type, b.Content, a.MemID, a.SubID, a.MainID, a.StockID, a.DirectorID, a.MemStake, a.MemResult, a.isPayOff, b.MemAccount, b.OneStake from `bet_BetList` a, `bet_BetListContent` b where a.ID > 0 and a.ID = b.ID ".$strSQL." order by a.ID desc";
}
//print "<!--".$strSQLT."//-->\n";


$intTolStake = 0;
$intTolResult = 0;
if($strSQL != ""){
	if($RS = @PMA_mysql_query($strSQLT, $$_Conn)){
		$Rows = PMA_mysql_fetch_object($RS);
		$intTotal = $Rows -> TotalItems;
		if(!($intTotal > 0))$intTotal = false;
	}

	$intTotalPage = $_data["totalpage"];
	$_data["Page"] = $_data["Page"];
	$intPageNo = (empty($_data["PageNo"]) || $isOpenGame)?100:$_data["PageNo"];												//一頁所要顯示的筆數
	$intTotalPage = ceil($intTotal / $intPageNo);		//總共幾頁
	$intPageNow = (empty($_data["Page"]))? 1:$_data["Page"];		//假如目前第幾頁為空值，其值為第一頁
	$intPageNow = ($intPageNow > $intTotalPage)? $intTotalPage:$intPageNow;	//目前第幾頁，假如大於總頁數，為總頁數
	$intPageNow = ($intPageNow < 1)? 1:$intPageNow;	//目前第幾頁，假如小於1，為第 1 頁
	if($intPageNow % 10 > 0){
		$intFirstPage = floor($intPageNow / 10)*10 + 1;
		$intLastPage = (floor($intPageNow / 10) + 1)*10;
	}else{
		$intFirstPage = floor(($intPageNow - 1) / 10)*10 + 1;
		$intLastPage = (floor(($intPageNow - 1) / 10) + 1)*10;
	}
	$intLastPage = ($intLastPage > $intTotalPage)? $intTotalPage:$intLastPage;

	$strSQL = $strSQL." limit ".(($intPageNow-1)*$intPageNo).",".$intPageNo." ";
	if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find Wager Data Error!";
		$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	$arrBID = array();
	$arrBet = array();
	$arrMemID = array();
	$arrMemSccount = array();
	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrBet[$Rows -> ID]["BetDate"] = DateForm($Rows -> BetDate, $Rows -> BetTime, "Y-m-d");
		$arrBet[$Rows -> ID]["BetTime"] = TimeForm($Rows -> BetDate, $Rows -> BetTime, "H:i:s");
		$arrBet[$Rows -> ID]["Bet_Type"] = $Rows -> Bet_Type;
		$arrBet[$Rows -> ID]["ScoreID"] = $Rows -> ScoreID;
		$arrBet[$Rows -> ID]["Score"] = $Rows -> Score;
		$arrBet[$Rows -> ID]["GambleID"] = $Rows -> GambleID;
		$arrBet[$Rows -> ID]["Content"] = $Rows -> Content;
		$arrBet[$Rows -> ID]["MemStake"] = $Rows -> MemStake;
		$arrBet[$Rows -> ID]["OneStake"] = $Rows -> OneStake;
		$arrBet[$Rows -> ID]["MemResult"] = $Rows -> MemResult;
		$arrBet[$Rows -> ID]["isPayOff"] = $Rows -> isPayOff;
		$arrBet[$Rows -> ID]["DirectorID"] = $Rows -> DirectorID;
		$arrBet[$Rows -> ID]["StockID"] = $Rows -> StockID;
		$arrBet[$Rows -> ID]["MainID"] = $Rows -> MainID;
		$arrBet[$Rows -> ID]["SubID"] = $Rows -> SubID;
		$arrBet[$Rows -> ID]["MemAccount"] = $Rows -> MemAccount;
		$arrBet[$Rows -> ID]["LottoID"] = $Rows -> LottoID;
		$arrMemID[$Rows -> SubID] = $Rows -> SubID;
		$arrMemID[$Rows -> MainID] = $Rows -> MainID;
		$arrMemID[$Rows -> StockID] = $Rows -> StockID;
		$arrMemID[$Rows -> DirectorID] = $Rows -> DirectorID;

		$arrBID[] = $Rows -> ID;
		if($Rows -> isPayOff >= 0){
			$intTolStake += $Rows -> MemStake;
			$intTolResult += $Rows -> MemResult;
		}
	}

	//找出所有會員帳號
	if(sizeof($arrMemID) > 0){
		$strSQL = "select ID, Account from `bet_Member` where ID in (".join(",", $arrMemID).")";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errorhidden"] = "Find User Data Error!";
			$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
			ErrMsgOutputHTML("Error", "", 1);
		}
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrMemAccount[$Rows -> ID] = $Rows -> Account;
		}
	}
}
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="/css/backend.css" type="text/css">
<script language="JavaScript1.2" type="text/javascript" src="/inc/admin_envirset.js"></script>
<script language="JavaScript">
<!--
var strRowClassName = ",datarow,";
function ShowLog(WagerID){
	window.open("ShowLog.php?WagerID="+WagerID,"ShowLogWindow","toolbar=no,width=540,height=400,top=0,left=0,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
}

function SelfSubmit(){
	with(document.beMemberFrm){
		submit();
	}
}
//-->
</script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #F7F7F7;
	font-size: 12px
}
.BetID{
	color: #FF0000;
	font-weight: 700;
}
//-->
</style>
</head>

<body>
<form action="<?php print $_SERVER["PHP_SELF"];?>" method="Get" name="beMemberFrm">
<input type="hidden" name="op" value="ChangeRiskBet">
<fieldset style="width: 500px">
	<legend>Search</legend>
	<table style="width: 800px; font-size: 12px">
	<tr>
		<td align="right"><?php print $_strSearchWagerList["WagerID"];?></td>
		<td align="left" colspan="7"><input type="text" name="BID" value="<?php print $_data["BID"];?>" size="50" class="input_text">(ps. 2, 33, ..., n)</td>
	</tr>
	<tr>
		<td align="right"><?php print $_strSearchWagerList["Lotto"];?></td>
		<td align="left"><?php print MakeLottoSelect($_arrLotto, $intLID, "LottoID", "SelfSubmit()", $_intLang);?></td>
		<td align="right"><?php print $_strSearchWagerList["WagerTime"];?></td>
		<td align="left" colspan="5">
			<input type="text" name="BetTime1" value="<?php print $_data["BetTime1"];?>" size="10" class="input_text"> ~ 
			<input type="text" name="BetTime2" value="<?php print $_data["BetTime2"];?>" size="10" class="input_text">(ps. 06:00:00 ~ 08:00:00)
		</td>
	</tr>
	<tr>
		<td align="right"><?php print $_strSearchWagerList["SerialNo"];?></td>
		<td align="left"><?php 
		if($isOpenGame){
			print $arrGD["GameDate"]."&nbsp;".$arrGD["SerialNo"];
		}else{
			print MakeGambleNoSelect($intLID, $_data["GambleID"], "GambleID", "", 4, $_arrLotto);
		}
		?></td>
		<td align="right"><?php print $_strSearchWagerList["Type"];?></td>
		<td align="left"><?php print SelectBetType($_data["Bet_Type"], "Bet_Type", "", $_arrLotto[$intLID]["LType"]);?></td>
		<td align="right"><?php print $_strSearchWagerList["WagerType"];?></td>
		<td align="left">
			<select name="WagerType">
				<option value="">--select--</option>
				<option value="0"<?php print ($_data["WagerType"] == "0")?" selected":"";?>><?php print $_strSearchWagerList["Common"];?></option>
				<option value="1"<?php print ($_data["WagerType"] == "1")?" selected":"";?>><?php print $_strSearchWagerList["Supple"];?></option>
			</select>
		</td>
		<td align="right"><?php print $_strSearchWagerList["WagerStatus"];?></td>
		<td align="left" colspan="2">
			<select name="WagerStatus">
				<option value="">--select--</option>
				<option value="0"<?php print ($_data["WagerStatus"] == "0")?" selected":"";?>><?php print $_strSearchWagerList["Accept"];?></option>
				<option value="-1"<?php print ($_data["WagerStatus"] == "-1")?" selected":"";?>><?php print $_strSearchWagerList["NotAccept"];?></option>
			</select>
		</td>
	</tr>
	<tr>
		<td align="right"><?php print $_strSearchWagerList["Member"];?></td>
		<td align="left"><input type="text" name="Mem" value="<?php print $_data["Mem"];?>" size="10" class="input_text"></td>
		<td align="right"><?php print $_strSearchWagerList["Sub"];?></td>
		<td align="left"><input type="text" name="Sub" value="<?php print $_data["Sub"];?>" size="10" class="input_text"></td>
		<td align="right"><?php print $_strSearchWagerList["Main"];?></td>
		<td align="left"><input type="text" name="Main" value="<?php print $_data["Main"];?>" size="10" class="input_text"></td>
		<td align="right"><?php print $_strSearchWagerList["Stock"];?></td>
		<td align="left"><input type="text" name="Stock" value="<?php print $_data["Stock"];?>" size="10" class="input_text"></td>
		<td align="right"><?php print $_strSearchWagerList["Director"];?></td>
		<td align="left"><input type="text" name="Director" value="<?php print $_data["Director"];?>" size="10" class="input_text"></td>
	</tr>
	<tr>
		<td align="right"><?php print $_strSearchWagerList["Stake"];?></td>
		<td align="left"><input type="text" name="Stake1" value="<?php print $_data["Stake1"];?>" size="10" class="input_text">~<input type="text" name="Stake2" value="<?php print $_data["Stake2"];?>" size="10" class="input_text"></td>
		<td align="right"><?php print $_strSearchWagerList["Result"];?></td>
		<td align="left"><input type="text" name="Result1" value="<?php print $_data["Result1"];?>" size="10" class="input_text">~<input type="text" name="Result2" value="<?php print $_data["Result2"];?>" size="10" class="input_text">
		</td>
		<td align="right"><?php print $_strSearchWagerList["ItemsOfPage"];?></td>
		<td align="left"><select name="PageNo">
		<option value="100"<?php if($_data["PageNo"] == 100)print " selected";?>>100</option>
<?php if($isOpenGame == false){ ?>
		<option value="200"<?php if($_data["PageNo"] == 200)print " selected";?>>200</option>
		<option value="5000"<?php if($_data["PageNo"] == 500)print " selected";?>>500</option>
		<option value="1000"<?php if($_data["PageNo"] == 1000)print " selected";?>>1000</option>
		<option value="1500"<?php if($_data["PageNo"] == 1500)print " selected";?>>1500</option>
		<option value="2000"<?php if($_data["PageNo"] == 2000)print " selected";?>>2000</option>
<?php } ?>
		</select><?php if($isOpenGame){ ?>(<a style="color: #FF0000">*<?php print $_strSearchWagerList["onOpen"];?></a>)<?php } ?>
		</td>
		<td align="right" colspan="3"><input type="submit" value="Go" class="submit">
		</td>
	</tr>
	</table>
</fieldset>
<br /><br />
<table class=content_b>
<tr>
<td valign="center" style="font-weight: 700; font-size: 16px"><?php print $_strSearchWagerList["Title"]?></td>
<td valign="top" align="right">
<select name="Page" onChange="document.beMemberFrm.submit()"><?php
	for($i = 1; $i <= $intTotalPage; $i++){
		?><option value="<?php print $i;?>"<?php print ($i == $intPageNow)?" selected":""?>><?php print $i;?></option><?php
	}
?></select>&nbsp;<a class="a_text" style="color: #7A96E9"><?php print $_strSearchWagerList["_Page"]."&nbsp;/&nbsp;".$_strSearchWagerList["_Sum"]."&nbsp;".$intTotalPage."&nbsp;".$_strSearchWagerList["_Page"]."&nbsp;/&nbsp;".$_strSearchWagerList["_Sum"]."&nbsp;".$intTotal."&nbsp;".$_strSearchWagerList["_No"];?></a>
</td></tr>
<tr>
<td valign="top" colspan="2">
<table border="0" cellpadding="0" cellspacing="0" bgcolor="#0054E3F"><tr><td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" style="font-size: 12px">
<tr bgcolor="#7A96E9" align="center">
<td width="120"><?php print $_strSearchWagerList["TimeTicketNo"];?></td>
<td width="80"><?php print $_strSearchWagerList["Director"];?></td>
<td width="80"><?php print $_strSearchWagerList["Stock"];?></td>
<td width="80"><?php print $_strSearchWagerList["Main"];?></td>
<td width="80"><?php print $_strSearchWagerList["Sub"];?></td>
<td width="80"><?php print $_strSearchWagerList["Member"];?></td>
<td width="70"><?php print $_strSearchWagerList["Type"];?></td>
<td width="220"><?php print $_strSearchWagerList["Content"];?></td>
<td width="80"><?php print $_strSearchWagerList["Stake"];?></td>
<td width="80"><?php print $_strSearchWagerList["Result"];?></td>
<td width="50"><?php print $_strSearchWagerList["Status"];?></td>
</tr>
<?php
while(list($k, $v) = each($arrBet)){
	$BetNo = abs($v["isPayOff"]);
?>
<tr class="datarow" bgcolor="#FFFFFF"><label for="isArray<?php print $k;?>">
<td align="left"><?php print $v["BetDate"];?><br /><?php print $v["BetTime"];?><br /><a class="BetID"><?php print $k;?></a></td>
<td align="left"><?php print $arrMemAccount[$v["DirectorID"]];?></td>
<td align="left"><?php print $arrMemAccount[$v["StockID"]];?></td>
<td align="left"><?php print $arrMemAccount[$v["MainID"]];?></td>
<td align="left"><?php print $arrMemAccount[$v["SubID"]];?></td>
<td align="left"><?php print $v["MemAccount"];?></td>
<td align="right"><?php print $_arrLotto[$v["LottoID"]]["Lotto_".$_intLang]."<br />".$_arrBet_Type[$_arrLotto[$v["LottoID"]]["LType"]][$v["Bet_Type"]];?></td>
<td align="left"><?php print WagerFormTrans($v["LottoID"], $v["Bet_Type"], $v["Content"]);?></td>
<td align="right"><?php
	if($v["Bet_Type"] >= 7 && $v["Bet_Type"] <= 11 && !empty($v["OneStake"])){
		if($v["MemStake"] / $v["OneStake"] > 1)print (string)$v["OneStake"]." x ".(string)round($v["MemStake"] / $v["OneStake"])."<br /><br />";
	}
	print $v["MemStake"];
?></td>
<td align="right"><?php print $v["MemResult"];?></td>
<td align="center"><?php
	switch($v["isPayOff"]){
		case -1:
			print "<b style=\"color:#FF0000\">".$_strSearchWagerList["NotAccept"]."</b>";
			break;
		case -2:
			print "<b style=\"color:#FF0000\">Not Deal</b>";
			break;			
		default:
			print $_strSearchWagerList["Accept"];
			break;
	}
?></td>
</tr>
<?php
}  //while(list($k, $v) = each($arrBet))//
?>
<tr bgcolor="#FFFFFF" align="right">
<td colspan="8"><?php print $_strSearchWagerList["Total"];?></td>
<td><?php print $intTolStake;?></td>
<td><?php print $intTolResult;?></td>
<td><?php print @sizeof($arrBet);?></td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php @mysql_close($$_Conn); ?>